#include "bkwidget.h"
#include <QApplication>
#include <QTranslator>
#include <QDebug>
#include <QFontDatabase>
#include <QDir>
#include <Windows.h>
QTranslator trans;


bool singleAppRunning() {
    //建立互斥锁保证一个程序运行
    BOOL bExist = FALSE;
    HANDLE hMutex = ::CreateMutex(NULL, TRUE, L"AnycubicUninstaller");

    if (GetLastError() == ERROR_ALREADY_EXISTS) {
        bExist = TRUE;
    }

    if (hMutex) {
        ::ReleaseMutex(hMutex);
    }

    if (bExist)
        return true;

    return false;
}


void loadQss()
{
    QFile file(":/qss/style.qss");
    if (!file.open(QFile::ReadOnly)) {
        qWarning() << "Can not found QSS file.";
        return;
    }
    QTextStream stream(&file);
    QString stylesheet = stream.readAll();
    file.close();

    qApp->setStyleSheet(stylesheet);
}

void loadFontFimily()
{
//    auto fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-Thin.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId);  qDebug() << "fontFamily Thin: " << fontFamilies; }
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-45-Light.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies; }
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-55-Regular.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies; }
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-55-RegularL3.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies; }
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-65-Medium.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies; }
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-75-SemiBold.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies; }
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-85-Bold.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies; }
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-95-ExtraBold.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies; }
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-105-Heavy.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies;}
//    fontId = QFontDatabase::addApplicationFont(QApplication::applicationDirPath() + "/asset/font/AlibabaPuHuiTi/AlibabaPuHuiTi-3-115-Black.otf");
//    if (fontId != -1) { QStringList fontFamilies = QFontDatabase::applicationFontFamilies(fontId); qDebug() << "fontFamily Thin: " << fontFamilies; }

//    QString sansCNFamily = "Alibaba PuHuiTi 3.0";

//    QFont defaultFont(sansCNFamily);
//    QApplication::setFont(defaultFont);
}

int main(int argc, char *argv[])
{
    QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
    QApplication app(argc, argv);
    QString appPath = QApplication::applicationDirPath();
    if(singleAppRunning())
        return -1;

    app.setWindowIcon(QIcon(":/res/PhotonSlicer.icns"));
    QDir::setCurrent(qApp->applicationDirPath());

    QLocale locale;
    QString langFilePath = appPath + "/translation_en.qm";
    if( locale.language() == QLocale::Chinese ) {
        langFilePath = appPath + "/translation_zh.qm";
    }

    bool bRet = trans.load(langFilePath);
    if(bRet){
        qApp->installTranslator(&trans);
    }

    loadQss();
    loadFontFimily();
    BkWidget w;
    w.show();
    return app.exec();
}
